<?php

class FileGestionnaireMessage extends GestionnaireMessage {
	private $msgDir;
	
	public function __construct($dir="/tmp/msg") {
		if (!is_dir($dir)) mkdir($dir,0777 , true);
		$this->msgDir=$dir;
	}
	
	private function parseMessage($f) {
		$content=file_get_contents($f);
		
		$ligne=split ("\n", $content);
		$titre=$ligne[0];
		$ligne[0]="";
		$message= implode ("\n", $ligne);
		return new Message($titre, $message);
	}
	public function recevoir() {
		$msg=NULL;
		if ($handle = opendir($this->msgDir)) {
    		while (false !== ($file = readdir($handle))) {
       		 $file=$this->msgDir."/$file";
       		 echo "* => $file\n";
       		 
       		 if (is_file($file) && strstr($file, ".msg" ) != FALSE ) {
            		echo "\t\t->Je traite $file\n";
            		$msg=$this->parseMessage($file);
	            	print_r($msg);
	            	unlink($file);
	            	break;
    	    	}
    		}
	    closedir($handle);
		}
		return $msg;
	}
	
	public function envoyer($msg){
	
	}
	public function mettreEnAttente($msg){
	
	}
}
?>
